package controllers.manage.report;

import controllers.BaseController;
import models.merchant.Merchant;
import models.weixin.WebUser;
import order.Order;
import order.OrderItem;
import org.apache.commons.collections.map.HashedMap;
import play.Logger;
import play.mvc.With;
import product.Product;
import product.ProductAccessRecord;
import product.type.ProductType;
import utils.DateUtil;

import java.util.*;

/**
 * Created by Administrator on 2018/4/8.
 */
@With(BaseController.class)
public class OrderTrendCtrl extends  BaseController {

    public static final Integer PAGE_SIZE = 10;

    /**
     *  商品销售统计
     * @param pageNumber
     * @param name
     */
    public static void index(Integer pageNumber, String name,String proParentType,String proType){

        Logger.info("商品销售统计------------ : %s","index");
        pageNumber = pageNumber == null ? 1 : pageNumber;
        Integer pageCurrent = pageNumber;
        pageCurrent = (pageCurrent-1)*PAGE_SIZE;

        Map<String , Object> searchMap = new HashMap<>();

        List<OrderItem> resultPage = OrderItem.getSaleStatistics(name,pageCurrent,PAGE_SIZE,proParentType,proType);
        List<OrderItem> allList = OrderItem.getSaleStatistics(name,null,null,proParentType,proType);
        Logger.info("resultPage.size------------ : %s",resultPage != null ? resultPage.size(): 0);
        int pageCount = 0;
        Logger.info("allList------------ : %s",resultPage != null ? resultPage.size(): 0);
        pageCount = allList.size() / PAGE_SIZE;
        if(pageCount != 0 && allList.size() % PAGE_SIZE != 0){
            pageCount = pageCount +1;
        }
        render(resultPage, pageNumber, name ,pageCount,proParentType,proType);

    }

    /*
      最近7天销售统计
     */
    public static void saleStatisticsByDay(){
        flash.put("menuId", "23241");
        render();
    }

    public static void getSaleStatisticsByDay(){

        Map<String,Object> resultMap = new HashedMap();
        Date endDate = new Date();
        ArrayList<String> pastDaysList = new ArrayList<>();
        int intervals = 7;
        for (int i = 0; i <intervals; i++) {
            pastDaysList.add(DateUtil.getPastDate(endDate,i));
        }
        List<Long> sumCount = new ArrayList<>();
        List<String> evDay = new ArrayList<>();
        for(String date : pastDaysList){
            sumCount.add(OrderItem.getSaleStatisticsByDay(DateUtil.stringToDate(date + " 00:00:00","yyyy-MM-dd HH:mm:ss"),DateUtil.stringToDate(date + " 23:59:59","yyyy-MM-dd HH:mm:ss")));
            evDay.add(date);
        }
        resultMap.put("evDay",evDay);
        resultMap.put("sumCount",sumCount);
        renderJSON(resultMap);
    }

    /**
     * 商品分类销售统计
     */
    public static void saleProdcutType(){
        flash.put("menuId", "23344");
        render();
    }

    public static void getSaleProdcutTypeStatistics(){
        Map<String,Object> resultMap = new HashedMap();
        List<ProductType> listProductType = ProductType.getSaleProdcutType();
        resultMap.put("listProductType",listProductType);
        renderJSON(resultMap);
    }

    /**
     * 当前状态表统计
     */
    public static void currentState(){
        flash.put("menuId", "23397");
        // 获取昨天到今天
        Date nowDay = new Date();
        String startTime = DateUtil.getPastDate(nowDay,1);
        String endTime = DateUtil.dateToString(nowDay,"yyyy-MM-dd");
        render(startTime,endTime);
    }

    public static void currentStateStatistics(String startTime,String endTime){

        Map<String,Object> resultMap = new HashedMap();

        Date beginDay = DateUtil.stringToDate(startTime + " 00:00:00","yyyy-MM-dd HH:mm:ss");
        Date endDay = DateUtil.stringToDate(endTime + " 23:59:59","yyyy-MM-dd HH:mm:ss");

        List<WebUser> webUserList = WebUser.findYesterday(beginDay,endDay);
        List<Merchant> merchantList = Merchant.findYesterday(beginDay,endDay);
        List<Product> productList = Product.getProductListByStatusAndDay(beginDay,endDay);
        List<Order> orderList = Order.findYesterdayByOrder(beginDay,endDay);
        List<Order> orderAmountList = Order.findYesterdayBySumAcount(startTime+ " 00:00:00",endTime + " 23:59:59");
        List<OrderItem> sumBuyNumber = OrderItem.findSumBuyNumberByDay(startTime+ " 00:00:00",endTime + " 23:59:59");
        List<ProductAccessRecord> productRecordList = ProductAccessRecord.findYesterdayByProduct(beginDay,endDay);
        resultMap.put("userCount",webUserList.size());
        resultMap.put("merchantCount",merchantList.size());
        resultMap.put("proCount",productList.size());
        resultMap.put("orderCount",orderList.size());
        resultMap.put("amountList",orderAmountList);
        resultMap.put("sumBuyNumber",sumBuyNumber);
        resultMap.put("proRecordCount",productRecordList.size());
        renderJSON(resultMap);
    }


    /**
     * 昨日数据
     */
    public static void saleYesterday(){
        flash.put("menuId", "23490");
        render();
    }

    public static void saleYesterdayStatistics(){
        Map<String,Object> resultMap = new HashedMap();
        // 获取昨天日期
        Date nowDay = new Date();
        String yesterDayStr = DateUtil.getPastDate(nowDay,1);
        Date yesterBeginDay = DateUtil.stringToDate(yesterDayStr + " 00:00:00","yyyy-MM-dd HH:mm:ss");
        Date yesterEndDay = DateUtil.stringToDate(yesterDayStr + " 23:59:59","yyyy-MM-dd HH:mm:ss");
        //昨日新增顾客数
        List<WebUser> webUserList = WebUser.findYesterday(yesterBeginDay,yesterEndDay);

        //昨日新增商户数
        List<Merchant> merchantList = Merchant.findYesterday(yesterBeginDay,yesterEndDay);

        //昨日访问客户数
        List<ProductAccessRecord> userRecordList = ProductAccessRecord.findYesterdayByWebUser(yesterBeginDay,yesterEndDay);

        //昨日访问数
        List<ProductAccessRecord> productRecordList = ProductAccessRecord.findYesterdayByProduct(yesterBeginDay,yesterEndDay);

        //昨日成交订单数
        List<Order> orderList = Order.findYesterdayByOrder(yesterBeginDay,yesterEndDay);

        //昨日成交额
        List<Order> sumAcountOrder = Order.findYesterdayBySumAcount(yesterDayStr + " 00:00:00",yesterDayStr + " 23:59:59");

        //昨日投诉订单数
        List<OrderItem> orderItemList = OrderItem.findYesterdayByTrialStatus(yesterBeginDay,yesterEndDay);

        resultMap.put("newUserCount",webUserList.size());
        resultMap.put("newMerCount",merchantList.size());
        resultMap.put("userRecordCount",userRecordList.size());
        resultMap.put("proRecordCount",productRecordList.size());
        resultMap.put("orderCount",orderList.size());
        resultMap.put("sumAcountOrder",sumAcountOrder);
        resultMap.put("tsOrderCount",orderItemList.size());

        renderJSON(resultMap);
    }


    public static void saleMonthlyStatistics(){
        flash.put("menuId", "23679");
        Map<String,Object> resultMap = new HashedMap();
        // 最近一个月时间
        Date nowDay = new Date();
        String beginDayStr = DateUtil.getPastDate(nowDay,30);
        Date nowDayEnd = DateUtil.getEndOfDay(nowDay);
        String nowDayEndStr = DateUtil.dateToString(nowDayEnd,"yyyy-MM-dd");
        Date beginBeginDay = DateUtil.stringToDate(beginDayStr + " 00:00:00","yyyy-MM-dd HH:mm:ss");
        //客户数量
        List<WebUser> webUserList = WebUser.findAvailable();

        //客户数量
        List<Merchant> merchantList = Merchant.findAll();

        //月访问量
        List<ProductAccessRecord> productRecordList = ProductAccessRecord.findYesterdayByProduct(beginBeginDay,nowDayEnd);

        //月成交订单
        List<Order> orderList = Order.findYesterdayByOrder(beginBeginDay,nowDayEnd);

        //月成交额度
        List<Order> sumAcountOrder = Order.findYesterdayByOrder(beginBeginDay,nowDayEnd);

        Double sumAcount = 0.00;
        for(Order order : sumAcountOrder){
            sumAcount += order.amount;
        }

        render(webUserList,merchantList,productRecordList,orderList,sumAcount);
    }





}
